﻿using IWshRuntimeLibrary;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UpdateServers.Command
{
    /// <summary>
    /// 自启启动
    /// </summary>
    public class AutoStartHelper
    {
        /// <summary>
        /// 获取自启路径
        /// </summary>
        private static readonly string AutoStartRoot = ConfigManager.GetAppSetting("AutoStartRoot");
        /// <summary>
        /// 设置自启
        /// </summary>
        /// <param name="isauto"></param>
        public static bool SetAutoStart(bool isauto, out string error)
        {
            var result = false;
            error = string.Empty;
            try
            {
                var PRO = Process.GetCurrentProcess();
                var lnk = $"{PRO.MainModule.ModuleName}.lnk";
                if (isauto)
                {
                    if (!Directory.Exists(AutoStartRoot))
                    {
                        Directory.CreateDirectory(AutoStartRoot);
                    }
                    //设置快捷方式到启动目录
                    CreateShortcut(AutoStartRoot, lnk, PRO.MainModule.FileName);
                    result = true;
                }
                else
                {
                    if (System.IO.File.Exists($"{AutoStartRoot}\\{lnk}"))
                    {
                        System.IO.File.Delete($"{AutoStartRoot}\\{lnk}");
                        result = true;
                    }
                    else
                    {
                        result = true;
                    }
                }

            }
            catch (Exception)
            {
                error = "设置权限不足！";
                result = false;
            }
            return result;

        }

        /// <summary>
        /// 创建快捷方式
        /// </summary>
        /// <param name="directory">快捷方式所处的文件夹</param>
        /// <param name="shortcutName">快捷方式名称</param>
        /// <param name="targetPath">目标路径</param>
        /// <param name="description">描述</param>
        /// <param name="iconLocation">图标路径，格式为"可执行文件或DLL路径, 图标编号"，
        /// 例如System.Environment.SystemDirectory + "\\" + "shell32.dll, 165"</param>
        /// <remarks></remarks>
        private static void CreateShortcut(string directory, string shortcutName, string targetPath,
            string description = null, string iconLocation = null)
        {
            if (!System.IO.Directory.Exists(directory))
            {
                System.IO.Directory.CreateDirectory(directory);
            }

            string shortcutPath = Path.Combine(directory, shortcutName);
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);//创建快捷方式对象
            shortcut.TargetPath = targetPath;//指定目标路径
            shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);//设置起始位置
            shortcut.WindowStyle = 1;//设置运行方式，默认为常规窗口
            shortcut.Description = description;//设置备注
            shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;//设置图标路径
            shortcut.Save();//保存快捷方式

        }

    }
}
